package com.example.menuvirtualclient.presenter;

import java.util.List;


import com.example.menuvirtualclient.core.*;
import com.example.menuvirtualclient.rule.*;
import com.example.menuvirtualclient.view.ITableView;

public class TablePresenter {

	private ITableView _view;
	private TableRule _rule;
	private OrderRule _orderRule;
	
	public TablePresenter(ITableView view){
		_view = view;
		_rule = new TableRule(view.GetContext());
	}
	
	public void LoadList(){
		try{
			_view.ShowTables(_rule.GetNames());
			//_view.ShowTables(_rule.GetAll());
		}
		catch(Exception ex){
			HandleException(ex);
		}
	}
	
	public void GetByName(String name){
		try{
			Table table = _rule.GetByName(name);
			_view.SetTableSelected(table);
			_view.CloseView();
		}
		catch(Exception ex){
			HandleException(ex);
		}
	}
	
	public void GetById(int id){
		try{
			Table table = _rule.Get(id);
			_view.SetTableSelected(table);
			_view.CloseView();
		}
		catch(Exception ex){
			HandleException(ex);
		}
	}
	
	public void SaveOrder(Order order){
		try{
			_orderRule.Save(order);
		}
		catch(Exception ex){
			HandleException(ex);
		}
	}
	
	private void HandleException(Exception ex){
		_view.SetMessage("Error", ex.getMessage());
	}
	
	@SuppressWarnings("unused")
	private void HandleException(String message){
		_view.SetMessage("Error", message);
	}
}
